/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.system.resource.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jkwzms.common.Constants;
import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.plugin.web.controller.BaseTreeableController;
import com.jkwzms.common.utils.EasyUiMsg;
import com.jkwzms.system.resource.entity.Resource;
import com.jkwzms.system.resource.entity.tmp.Menu;
import com.jkwzms.system.resource.entity.tmp.Treegrid;
import com.jkwzms.system.resource.service.ResourceService;

/**
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-1-28 下午4:29
 * <p>
 * Version:1.0.0
 */
@Controller
@RequestMapping(value = "/sys/resource")
public class ResourceController extends BaseTreeableController<Resource, Long> {

    @Autowired
    @BaseComponent
    private ResourceService resourceService;

    public ResourceController(){
        setResourceIdentity("sys:resource");
    } 

    @RequestMapping(value = "main", method = RequestMethod.GET)
    public String main() {
        return viewName("list");
    }

    @RequestMapping(value = "showIcons", method = RequestMethod.GET)
    public String showIcons() {
        return viewName("icons");
    }

    @RequestMapping(value = "treegrid", method = RequestMethod.POST)
    @ResponseBody
    public List<Treegrid> treegrid() {
        return this.resourceService.findTreegrids();
    }

    @RequestMapping(value = "combotree", method = RequestMethod.POST)
    @ResponseBody
    public List<Menu> combotree() {
        return this.resourceService.findcombotrees();
    }

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(Model model) {
        setCommonData(model);
        if (!model.containsAttribute("m")) {
            model.addAttribute("m", newModel());
        }
        return viewName("editForm");
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(Model model, @Valid @ModelAttribute("m") Resource m, BindingResult result) {
        if (permissionList != null) {
            this.permissionList.assertHasCreatePermission();
        }

        if (hasError(m, result)) {
            return EasyUiMsg.successMsg(false);
        }

        this.resourceService.save(m);
        return EasyUiMsg.successMsg(true);
    }

    @RequestMapping(value = "update/{id}")
    public String showUpdateForm(@PathVariable("id") Resource m, Model model) {
        // 判断权限
        if (permissionList != null) {
            this.permissionList.assertHasUpdatePermission();
        }
        setCommonData(model);
        model.addAttribute("m", m);
        return viewName("editForm");
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(Model model, @ModelAttribute("m") Resource m, BindingResult result) {

        if (permissionList != null) {
            permissionList.assertHasUpdatePermission();
        }
        if (result.hasErrors()) {
            return EasyUiMsg.ajaxMsg(false, "资源信息更新失败");
        }
        this.resourceService.update(m);
        return EasyUiMsg.successMsg(true);
    }

    /**
     * 该方法未使用目前
     */
    @RequestMapping(value = "/changeStatus/{newStatus}")
    public String changeStatus(HttpServletRequest request, @PathVariable("newStatus") Boolean newStatus,
                               @RequestParam("ids") Long[] ids, RedirectAttributes redirectAttributes) {

        if (permissionList != null) {
            this.permissionList.assertHasUpdatePermission();
        }

        for (Long id : ids) {
            Resource resource = resourceService.findOne(id);
            resource.setShow(newStatus);
            resourceService.update(resource);
        }

        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "操作成功！");

        return "redirect:" + request.getAttribute(Constants.BACK_URL);
    }

}
